<?php
/**************************************************************************
 * Naanal PHP Framework, Simple, Efficient and Developer Friendly
 * Ver 3.0, Copyright (C) <2010>  <Tamil Amuthan. R>
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ************************************************************************/
include_once dirname(__FILE__)."/ClsNaanalmodconf.php";
include_once dirname(__FILE__)."/ClsNaanalmodcontr.php";
include_once dirname(__FILE__)."/ClsNaanalmodwrap.php";
class ClsNaanalmod
{
    private $name="";
    private $objConfig=null;
    private $objController=null;
    private $objWrapper=null;
    public function __construct($name,$database=false,$table=false)
    {
        $this->name=$name;
        $this->objConfig=new ClsNaanalmodconf($database,$table);
    }
    public function &getConfig()
    {
        return $this->objConfig;
    }
    public function &getController()
    {
        return $this->objController;
    }
    public function &getWrapper()
    {
        return $this->objWrapper;
    }
    public function render()
    {
        $module=  $this->name;
        $moduleDirectoryPath=NAANAL_APP_PATH."modules/{$module}/";
        $this->objController=new ClsNaanalmodcontr($this->name,$this->objConfig);
        $this->objWrapper=new ClsNaanalmodwrap($this->name,$this->objConfig);
        try
        {
            if(!file_exists(NAANAL_APP_PATH."modules"))
            {
                if(is_writable(NAANAL_APP_PATH))
                {
                    mkdir(NAANAL_APP_PATH."modules");
                }
                else
                {
                    errMsg("application directory does not have enough permission");
                    $this->redirect="{$this->urlModuleParam}=naanaladmin";
                    return;
                }
            }
            if(file_exists(NAANAL_APP_PATH."modules") && !is_writable(NAANAL_APP_PATH."modules"))
            {
                errMsg("module directory does not have enough permission");
                $this->redirect="{$this->urlModuleParam}=naanaladmin";
                return;
            }
            if(!file_exists($moduleDirectoryPath))
            {
                $success=mkdir($moduleDirectoryPath);
                if($success!==true)
                {
                    errMsg("unable to create new module directory");
                    $this->redirect="{$this->urlModuleParam}=naanaladmin";
                    return;
                }
            }
            $filepath= "{$moduleDirectoryPath}config.php";  
            $actions=ClsNaanalRequest::getInstance()->getData("actions");
            $arrAction=array();
            if(!empty($actions))
            {
                $arrAction=explode(",",$actions);
                foreach($arrAction as $ind=>$action)
                {
                    $arrAction[$ind]=trim($action);
                }
            }
            if(!file_exists($filepath))
            {
                foreach($arrAction as $action)
                {
                    $this->objConfig->addAction($action);
                }

                $template_variables=ClsNaanalRequest::getInstance()->getData("template_variables");
                if(!empty($template_variables))
                {
                    $arrTemplateVariables=explode(",",$template_variables);
                    foreach($arrTemplateVariables as $ind=>$variable)
                    {
                        $arrTemplateVariables[$ind]=trim($variable);
                    }
                    foreach($arrTemplateVariables as $variable)
                    {
                        $this->objConfig->addTemplateVariable($variable);
                    }
                }

                $function_calls=ClsNaanalRequest::getInstance()->getData("function_calls");
                if(!empty($template_variables))
                {
                    $arrFunctionCalls=explode(",",$function_calls);
                    foreach($arrFunctionCalls as $ind=>$function)
                    {
                        $arrFunctionCalls[$ind]=trim($function);
                    }
                    foreach($arrFunctionCalls as $function)
                    {
                        $this->objConfig->addFunctionCall($function);
                    }
                }

                if($handle=fopen($filepath, "w"))
                {
                    if (fwrite($handle, $this->objConfig->render()) === FALSE) 
                    {
                        errMsg("Cannot write to module's controller");
                        $this->redirect="{$this->urlModuleParam}=naanaladmin";
                        fclose($handle);
                        return;
                    }
                }
                else
                {
                    errMsg("unable to create module's controller file");
                    $this->redirect="{$this->urlModuleParam}=naanaladmin";
                    return;
                }
            }
            
            /**
            * create action files
            */
            foreach($arrAction as $action)
            {
                if(!file_exists($moduleDirectoryPath.$action.".php"))
                {
                    file_put_contents($moduleDirectoryPath.$action.".php", "");
                }
            }
            
            /**
            * create controller
            */
            $controller="Cls".ucfirst($module);
            $filepath= NAANAL_APP_PATH."modules/{$module}/{$controller}.php";
            if(!file_exists($filepath))
            {          
                if($handle=fopen($filepath, "w+"))
                {
                    if (fwrite($handle, $this->objController->render()) === FALSE) 
                    {
                        errMsg("Cannot write to module's controller");
                        $this->redirect="{$this->urlModuleParam}=naanaladmin";
                        return;
                    }
                }
                else
                {
                    errMsg("unable to create module's controller");
                    $this->redirect="{$this->urlModuleParam}=naanaladmin";
                    return;
                }
            }
            
            /**
            * create wrapper
            */
            $wrapper="ClsW".ucfirst($module);
            $filepath= NAANAL_APP_PATH."modules/{$module}/{$wrapper}.php";
            if(!file_exists($filepath))
            {          
                if($handle=fopen($filepath, "w"))
                {
                    if (fwrite($handle, $this->objWrapper->render()) === FALSE) 
                    {
                        errMsg("Cannot write to module's wrapper");
                        $this->redirect="{$this->urlModuleParam}=naanaladmin";
                        return;
                    }
                }
                else
                {
                    errMsg("unable to create module's wrapper");
                    $this->redirect="{$this->urlModuleParam}=naanaladmin";
                    return;
                }
            }
            if(!file_exists(NAANAL_APP_PATH."modules/{$module}/lang"))
            {
                mkdir(NAANAL_APP_PATH."modules/{$module}/lang");
            }
            if(!file_exists(NAANAL_APP_PATH."modules/{$module}/lang/default"))
            {
                mkdir(NAANAL_APP_PATH."modules/{$module}/lang/default");
            }
            if(!file_exists(NAANAL_APP_PATH."modules/{$module}/lang/default/common.php"))
            {
                file_put_contents(NAANAL_APP_PATH."modules/{$module}/lang/default/common.php","");
            }
            $this->redirect="{$this->urlModuleParam}=naanaladmin";
        }
        catch(Exception $e)
        {
            trace($e);
        }
    }
}
?>